import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DeleteCommand implements Command {
	
	
	
	private static final String DELETE = "delete";
	private static final String ALL = "all";
	public ArrayList<Task> tasks;
	public AddParser addParser;
	public Storage storage;
	public History history;
	public Logger logger;
	String data;
	String errorMessage;
	String output;
	boolean isError = false;

	private static final String DELETE_SUCCESSFUL = "Deleted Task(s) %s\n";
	private static final String DELETE_UNSUCCESSFUL_INVALID_INDEX = "Task %d is not found.\n";
	private static final String DELETE_ALL_SUCCESSFUL = "All tasks deleted\n";
	private static final String ERROR_TASK_ID_IS_NOT_NUMBER = "Task ID must be a number.\n";
	private static final String EMPTY = "";
	private static final int BASEINDEX = 1;
	private static final int INITAL_INDEX = -1;
	private static final String SPACING = " ";

	public DeleteCommand(ArrayList<Task> tasks, Storage storage, History history,
			Logger logger) {
		this.tasks = tasks;
		this.addParser = new AddParser();
		this.storage = storage;
		this.history = history;
		this.logger = logger;
		errorMessage = EMPTY;
	}

	/*
	 		*/

	@Override
	public void acceptInput(String data) {
		// TODO Auto-generated method stub
		this.data = data;

	}

	@Override
	public void execute() {
		// TODO Auto-generated method stub
		int id = INITAL_INDEX;
		String[] dataComponents;
		String allTasksToDel = EMPTY;
		
		data = data.trim();

		if (data.equals(ALL)) {
			history.storeUndo(tasks, DELETE);
			tasks.clear();
			output = DELETE_ALL_SUCCESSFUL;
		} else {
			dataComponents = data.split(SPACING);
			ArrayList<Integer> tasksIndexToDel = new ArrayList<Integer>();
			ArrayList<Task> tasksToDel = new ArrayList<Task>();
			try {
				for (int i = 0; i < dataComponents.length && !isError; i++) {
					id = Integer.parseInt(dataComponents[i]);
					if (isIDValid(id)) {
						tasksIndexToDel.add(id);
						allTasksToDel += SPACING;
						allTasksToDel += id;
					} else {
						output = String.format(
								DELETE_UNSUCCESSFUL_INVALID_INDEX, id);
						isError = true;
					}
				}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.log(Level.INFO, ERROR_TASK_ID_IS_NOT_NUMBER);
				errorMessage = ERROR_TASK_ID_IS_NOT_NUMBER;
			}

			for (int i = 0; i < tasksIndexToDel.size() && !isError; i++) {
				tasksToDel.add(tasks.get(tasksIndexToDel.get(i) - BASEINDEX));
			}
			if(!isError){
			history.storeUndo(tasks, DELETE);
			}
			for (int i = 0; i < tasksToDel.size() && !isError; i++) {
				tasks.remove(tasksToDel.get(i));
			}
			if(!isError){
			output = String.format(DELETE_SUCCESSFUL,allTasksToDel);
			}
		}

	}

	@Override
	public String save() throws IOException {
		// TODO Auto-generated method stub
		return storage.writeToStorage(tasks);
	}

	@Override
	public String outputToUser() {
		// TODO Auto-generated method stub

		if (!errorMessage.equals(EMPTY)) {
			output = errorMessage;
			isError = true;
		}else {
			isError = false;
		}
		
		return output;
	}

	// check if the original ID given by the user is valid. NOTE: ID is not
	// adjusted to base 0 of ArrayList
	private boolean isIDValid(int id) {

		if (tasks.size() >= 1) {
			if (id >= 1 && id <= tasks.size()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean isError() {
		// TODO Auto-generated method stub
		return isError;
	}

}
